package article_management;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
/*
 * 
 * */
public class TestingConnection {
	private static final Array Array = null;
	private String driverName="oracle.jdbc.OracleDriver";
	private final String url="jdbc:oracle:thin:@210.115.182.197:1521:orcl";
	private final String user="sr06";
	private final String pwd="sr06";
	private ResultSet rs;
	private PreparedStatement pstmt;
	private EstablishConn con;
	public TestingConnection(){
			con=new EstablishConn(driverName,url,user,pwd);
			
			//insertNew();
			displayData();
		   //update();
			//delete();
			//search();
	}
	public void displayData(){
		String sql="Select * From Articles Where Art_ID >0";
		try{
			pstmt=con.getCon().prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()){
				getData();
			}
			pstmt.close();
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	public void getData(){
		try{
			System.out.println(rs.getInt(1));
			System.out.println(rs.getString(2));
			System.out.println(rs.getString(3));
			System.out.println(rs.getString(4));
			System.out.println(rs.getString(5));}
			catch (SQLException e) {}
	}
	public void insertNew(){
		String sql="Insert Into Articles Values((Select to_number(0||max(art_id)+1)From Articles),?,?,?,?)";
		try{
			pstmt=con.getCon().prepareStatement(sql);
			pstmt.setString(1, "b");
			pstmt.setString(2, "b");
			pstmt.setString(3, "b");
			pstmt.setString(4, "b");
			pstmt.executeUpdate();
			pstmt.close();
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	public void update(){
		String sql = "";
		String field[] ={"Art_Title","Art_Author","Art_Content"};
		for(int i=0;i<field.length;i++){
			sql="Update Articles Set "+field[i]+"=? Where art_id=?";
		}
		try{
			pstmt=con.getCon().prepareStatement(sql);	
			pstmt.setObject(1, "dd");
			pstmt.setInt(2, 4);
			pstmt.executeUpdate();
			pstmt.close();
		}catch(SQLException e){
			
		}
	}
	public void delete(){
		String sql="Delete From Articles Where Art_ID=?";
		try{
			pstmt=con.getCon().prepareStatement(sql);
			pstmt.setInt(1, 5);
			pstmt.executeUpdate();
			pstmt.close();
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	public void search(){
		String word="7";
		String sql="Select * From Articles Where Art_ID like ? "
				+ "or Art_Title like ? or Art_Author like ? "
				+ "or Art_Date like ?";
		try{
			pstmt=con.getCon().prepareStatement(sql);
			pstmt.setString(1, "%"+word+"%");
			pstmt.setString(2, "%"+word+"%");
			pstmt.setString(3, "%"+word+"%");
			pstmt.setString(4, "%"+word+"%");
			rs=pstmt.executeQuery();
			while(rs.next()){
				getData();
			}
			pstmt.close();
			rs.close();
		}catch(SQLException e){e.printStackTrace();}
	}
	
	public static void main(String args[]){
		new TestingConnection();
	}
}
